var DEBUG = false,QOM='xinhujavaweb_',NOWURL='',maincolor='#4D85FF',HOST,device='',CFROM='pc',token='',isIE=true,theme='base',nwjs=false,rockbool=false;
function initbody(){}
function initrock(){}
$(document).ready(function(){
	if(!document.all)isIE=false;
	HOST = js.gethost();
	token= js.getoption('token');
	initbody();
	
	if(HOST=='127.0.0.1' || HOST=='localhost' || HOST.indexOf('192.168.')>-1)window.addEventListener('error',function(e){
		var msg = '文件：'+e.filename+'<br>行：'+e.lineno+'<br>错误：<font color=red>'+e.message+'</font>';
		js.alert(msg);
	});
	
	$('body').keydown(function(e){
		var code	= e.keyCode;
		if(code==27){
			var objt = $('div[tanbodynew]');
			if(objt.length>0){
				js.tanclose($(objt[objt.length-1]).attr('tanbodynew'));
				return false;
			}
		}
		if(js.onkeydown(code,e))return false;
	});
	if(typeof(nw)=='object')nwjs=true;
	
});
var js={};
var get=function(id){return document.getElementById(id)};
var isempt=function(an){var ob	= false;if(an==''||an==null||typeof(an)=='undefined'){ob=true;}if(typeof(an)=='number'){ob=false;}return ob;}
var jsform=function(an,fna){if(!fna)fna='myform';return document[fna][an]}
var replacebr=function(v){return (typeof(v)!='undefined') ? v.replace(/\n/gi,'<br>') : '';}

js.onkeydown = function(){return false;}
js.setoption = function(k,v,qzb){
	if(!qzb)k=QOM+k;
	if(isempt(v)){
		localStorage.removeItem(k);
	}else{
		if(typeof(v)=='object')v=JSON.stringify(v);
		localStorage.setItem(k, escape(v));
	}
	return true;
}

js.debug=function(str){
	if(DEBUG)console.log(''+this.now()+': '+str+'');
}

js.getoption=function(k,dev, qzb){
	var s = '';
	if(!qzb)k=QOM+k;
	s = localStorage.getItem(k);
	if(s)s=unescape(s);
	if(isempt(dev))dev='';
	if(isempt(s))s=dev;
	return s;
}


js.gethost=function(){
	var url = location.href,sau='';
	try{sau = url.split('//')[1].split('/')[0];}catch(e){}
	var lse = url.lastIndexOf('/');NOWURL = url.substr(0, lse+1);
	QOM		= NOWURL.replace(/\./g,'').replace(/\//g,'').replace(/\:/g,'')+'_';
	device  = this.getoption('deviceid');
	if(!device){
		device=js.now('time');
		this.setoption('deviceid',device);
	}
	return sau;
}
function winHb(){
	var winH=(!isIE)?window.innerHeight:document.documentElement.offsetHeight;
	return winH;
}
function winWb(){
	var winH=(!isIE)?window.innerWidth:document.documentElement.offsetWidth;
	return winH;
}

js.now=function(type,sj){
	if(!type)type='Y-m-d';
	if(type=='now')type='Y-m-d H:i:s';
	var dt,ymd,his,weekArr,Y,m,d,w,H=0,i=0,s=0,W;
	if(typeof(sj)=='string')sj=sj.replace(/\//gi,'-');
	if(/^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}/.test(sj)){
		sj=sj.split(' ');
		ymd=sj[0];
		his=sj[1];if(!his)his='00:00:00';
		ymd=ymd.split('-');
		his=his.split(':');
		H = his[0];if(his.length>1)i = his[1];if(his.length>2)s = his[2];
		dt=new Date(ymd[0],ymd[1]-1,ymd[2],H,i,s);
	}else{
		dt=(typeof(sj)=='number')?new Date(sj):new Date();
	}
	var xy10=function(s){var s1=''+s+'';if(s1.length<2)s1='0'+s+'';return s1;};
	weekArr=new Array('日','一','二','三','四','五','六');
	Y=dt.getFullYear();
	m=xy10(dt.getMonth()+1);
	d=xy10(dt.getDate());
	w=dt.getDay();
	H=xy10(dt.getHours());
	i=xy10(dt.getMinutes());
	s=xy10(dt.getSeconds());
	W=weekArr[w];
	if(type=='time'){
		return dt.getTime();
	}else{
		return type.replace('Y',Y).replace('m',m).replace('d',d).replace('H',H).replace('i',i).replace('s',s).replace('w',w).replace('W',W);
	}
}

js.splittime=0;
js.getsplit=function(){
	if(!js.servernow)return false;
	var dt=js.now('Y-m-d H:i:s');
	var d1=js.now('time',dt);	
	var d2=js.now('time',js.servernow);
	js.splittime=d1-d2;
}
js.serverdt=function(atype){
	if(!atype)atype='Y-m-d H:i:s';
	var d1=js.now('time')-js.splittime;
	var dt=js.now(atype,d1);
	return dt;
}

js.location = function(url){
	location.href = url;
}

js.reload = function(){
	location.reload();
}

js.getarr = function(arr){
	console.log(arr);
	alert(JSON.stringify(arr));
}

js.request=function(name,dev,url){
	this.requestarr = {};
	if(!dev)dev='';
	if(!name)return dev;
	if(!url)url=location.href;
	if(url.indexOf('\?')<0)return dev;
	if(url.indexOf('#')>0)url = url.split('#')[0];
	var neurl=url.split('\?')[1];
	neurl=neurl.split('&');
	var value=dev,i,val;
	for(i=0;i<neurl.length;i++){
		val=neurl[i].split('=');
		this.requestarr[val[0]] = val[1];
		if(val[0].toLowerCase()==name.toLowerCase()){
			value=val[1];
			break;
		}
	}
	if(!value)value='';
	return value;
}

js.getrand=function(){
	var r;
	r = ''+new Date().getTime()+'';
	r+='_'+parseInt(Math.random()*9999)+'';
	return r;
}

js.importjskey = {}
js.importjs=function(url,fun){
	var sid  = jm.base64encode(url.split('abckey')[0]);
	if(!fun)fun=function(){};
	if(get(sid)){fun({src:url});return;}
	var scr  = document.createElement('script');
	var jg 	 = (url.indexOf('?')==-1) ? '?':'&';
	if(url.indexOf('?rock')==-1)url+=''+jg+'rnd='+this.getrand()+'';
	scr.src  = url;
	scr.type = 'text/javascript';
	scr.id 	 = sid;
	if(isIE){
		scr.onreadystatechange = function(){if(this.readyState=='loaded' || this.readyState=='complete'){fun(this);}}
	}else{
		scr.onload = function(){fun(this);}
		scr.onerror= function(){fun(this);}
	}
	document.getElementsByTagName('head')[0].appendChild(scr);
	return false;	
}

var importjs = function(na, fun, cans){
	var dz = 'res/jsplugin/jsplugin_'+na+'.js?name='+na+'';
	var key = 'key'+js.getrand()+'';
	js.importjs[key] = {na:na,fun:fun,cans:cans};
	dz+='&abckey='+key+'';
	js.importjs(dz, function(o){
		var ky = js.request('abckey','', o.src);
		if(ky){
			var obj = js.importjs[ky];
			var na  = obj.na,fun = obj.fun,cans = obj.cans;
			if(js[na] && js[na].init){js[na].init();js[na].init=false;}
			if(typeof(fun)=='function')fun();
			if(typeof(fun)=='string'){
				var dobj = new js[na][fun](cans);
				if(cans && cans.nowobj && cans.nowcls)cans.nowobj[cans.nowcls]=dobj;
			}
			js.importjs[ky]= null;
		}
	});
}

var importcss= function(na){
	js.importcss('res/theme/'+theme+'/'+na+'.css');
}

/*
var importicons=function(){
	alert(1);
	js.importcss('res/fontawesome/css/font-awesome.min.css?rock');
}
*/

js.importcss = function(url){
	var sid  = jm.base64encode(url);
	if(get(sid))return;
	var scr  = document.createElement('link');
	if(url.indexOf('?')==-1)url+='?'+this.getrand()+'';
	scr.href = url;
	scr.id   = sid;
	scr.rel  = 'stylesheet';
	document.getElementsByTagName('head')[0].appendChild(scr);
}

js.initbtn = function(obj,view){
	var i,o1,cl;
	var o = (view) ? $('#'+view+'').find("[clickevt]") : $("[clickevt]");
	for(i=0; i<o.length; i++){
		o1	= $(o[i]);
		cl	= o1.attr('clickadd');
		if(cl!='true'){
			var busfun = function(eo){
				var cls = $(this).attr('clickevt');
				if(typeof(cls)=='string'){
					cls=cls.split(',');
					obj[cls[0]](this, cls[1], cls[2], eo);
				}
				return false;
			}
			o1.click(busfun);
		}
	}
	if(obj.init && !view)obj.init();
	o.attr('clickadd','true');
}

js.applyIf=function(a,b){
	if(!a)a={};if(!b)b={};
	for(var c in b)if(typeof(a[c])=='undefined')a[c]=b[c];
	return a;
}

js.apply=function(a,b){
	if(!a)a={};if(!b)b={};
	for(var c in b)a[c]=b[c];
	return a;
}

js.decode=function(str){
	var arr	= {length:-1};
	try{
		arr	= new Function('return '+str+'')();
	}catch(e){}
	return arr;
}

js.focusval	= '0';
js.number=function(obj){
	var val=obj.value;
	if(!val){
		obj.value=js.focusval;
		return false;
	}
	if(isNaN(val)){
		js.load.msg('输入的不是数字');
		obj.value=js.focusval;
		obj.focus();
	}else{
		var o1 = $(obj);
		var min= min= o1.attr('min');
		if(min && parseFloat(val)<parseFloat(min))val=min;
		var max= max= o1.attr('max');
		if(max && parseFloat(val)>parseFloat(max))val=max;
		obj.value=val;
	}
}

js.setmsg=function(txt,col,ids){
	if(!ids)ids='msgview';
	$('#'+ids+'').html(js.getmsg(txt,col));
}
js.getmsg  = function(txt,col){
	if(!col)col='red';
	var s	= '';
	if(!txt)txt='';
	if(txt.indexOf('...')>0){
		s='<i  class="rock-loading"></i> ';
		col = '#ff6600';
	}	
	s+='<span style="color:'+col+'">'+txt+'</span>';
	if(!txt)s='';
	return s;
}



js.selall = function(o,na,bh){
	var i,oi1;
	if(bh){
		o1=$("input[name^='"+na+"']");
	}else{
		o1=$("input[name='"+na+"']");
	}
	for(i=0;i<o1.length;i++){
		if(!o1[i].disabled)o1[i].checked = o.checked;
	}
}
js.getchecked=function(na,bh){
	var s = '';
	var o1;
	if(bh){
		o1=$("input[name^='"+na+"']");
	}else{
		o1=$("input[name='"+na+"']");
	}
	for(var i=0;i<o1.length;i++){
		if(o1[i].checked && !o1[i].disabled)s+=','+o1[i].value+'';
	}
	if(s!='')s=s.substr(1);
	return s;
}
js.setselectdata = function(o, data, vfs, devs){
	var i,ty = data,sv,str='';
	if(!data)return;	
	if(!vfs)vfs='name';	
	if(typeof(devs)=='undefined')devs='&nbsp;';
	for(i=0;i<ty.length;i++){
		if(ty[i].optgroup){
			if(ty[i].optgroup=='start')str+='<optgroup label="'+ty[i].name+'">';
			if(ty[i].optgroup=='end')str+='</optgroup>';
		}else{
			str+='<option value="'+ty[i][vfs]+'">'+ty[i].name+'</option>';
		}
	}
	$(o).append(str);
}

js.getformdata=function(nas){
	var da	={},ona='',o,type,val,na,i,obj;
	if(!nas)nas='myform';
	obj	= document[nas];
	for(i=0;i<obj.length;i++){
		o 	 = obj[i];type = o.type,val = o.value,na = o.name;
		if(!na)continue;
		if(type=='checkbox'){
			val	= '0';
			if(o.checked)val='1';
			da[na]	= val;
		}else if(type=='radio'){
			if(o.checked)da[na]	= val;					
		}else{
			da[na] = val;
		}
		if(na.indexOf('[]')>-1){
			if(ona.indexOf(na)<0)ona+=','+na+'';
		}
	}
	if(ona != ''){
		var onas = ona.split(',');
		for(i=1; i<onas.length; i++){
			da[onas[i].replace('[]','')] = js.getchecked(onas[i]);
		}
	}
	return da;
}

js.formatsize=function(size){
	var arr = new Array('Byte', 'KB', 'MB', 'GB', 'TB', 'PB');
	var e	= Math.floor(Math.log(size)/Math.log(1024));
	var fs	= size/Math.pow(1024,Math.floor(e));
	return js.float(fs)+' '+arr[e];
}

js.float=function(num,w){
	if(isNaN(num)||num==''||!num||num==null)num='0';
	num=parseFloat(num);
	if(!w&&w!=0)w=2;
	var m=num.toFixed(w);
	return m;	
}

js.viewimg=function(o1){
	importjs('imgview','create',{
		obj:o1,
		ismobile:js.web.ismobile()
	});
}

js.openarr = {}
js.open = function(url,w,h,wina,can,wjcan){
	if(wina){try{var owina	= this.openarr[wina];owina.document.body;owina.focus();return owina;}catch(e){}}
	if(!w)w=750;if(!h)h=500;
	var l=(screen.width-w)*0.5,t=(screen.height-h)*0.5-50,rnd = parseInt(Math.random()*50);
	if(rnd%2==0){l=l+rnd;t=t-rnd;}else{l=l-rnd;t=t+rnd;}
	if(!can)can={};
	var s='resizable=yes,scrollbars=yes,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,location=no,status=no';
	var a1={'left':''+l+'px','top':''+t+'px','width':''+w+'px','height':''+h+'px'};
	a1 = js.apply(a1,can);
	for(var o1 in a1)s+=','+o1+'='+a1[o1]+'';
	var ja=(url.indexOf('?')>=0)?'&':'?';
	if(wina)url+=''+ja+'winobj='+wina+'';
	var opar=window.open(url,'',s);
	if(wina)this.openarr[wina]=opar;
	return false;
}

js.changedate = function(o1, lx){
	importjs('datepicker','open',{
		inputobj:o1,
		view:lx
	});
}

getface = function(mx){
	if(!mx)mx='images/noface.png';
	return mx;
}

js.isimg = function(lx){
	var ftype 	= '|png|jpg|bmp|gif|jpeg|';
	var bo		= false;
	if(ftype.indexOf('|'+lx+'|')>-1)bo=true;
	return bo;
}

js.back = function(){
	history.back();
}

//app使用的
rockready = function(){
	rockbool = true;
	device	 = rock.deviceId;
	CFROM	 = rock.appType+'app';
	initrock();
	importjs('rockapp');
}